package com.itbaizhan.openhis.controller.doctor;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

import com.itbaizhan.openhis.config.pay.AlipayConfig;
import com.itbaizhan.openhis.constant.Constants;
import com.itbaizhan.openhis.service.OrderChargeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("pay")
public class PayController {

    @Reference
    private OrderChargeService orderChargeService;

    static Log log = LogFactory.getLog("payController");


    /**
     *
     * @param orderId 订单id,付款后订单的状态改变
     * @param request
     */
    @PostMapping("callback/{orderId}")
    public void callback(@PathVariable String orderId, HttpServletRequest request){


        try {
            Map<String,String> parameterMap = this.getParameterMap(request);
            String trade_status = parameterMap.get("trade_status");
            //判断是退费还是支付
            if("TRADE_SUCCESS".equals(trade_status)){
                boolean checkV1 =AlipaySignature.rsaCheckV1(parameterMap, AlipayConfig.alipay_public_key,AlipayConfig.charset,AlipayConfig.sign_type);
                log.info("验证结果"+checkV1);
                String refund_fee = parameterMap.get("refund_fee");//退费金额
                if(StringUtils.isNotBlank(refund_fee)){
                    //说明是退费
                    System.out.println("退费成功，退费的子订单号是"+parameterMap.get("out_biz_no"));
                }else {
                    //说明是支付
                    System.out.println("收费成功，平台id"+parameterMap.get("trade_no"));
                    //通过回调方法，把订单主表与订单详情更新
                    String trade_no = parameterMap.get("trade_no");
                    orderChargeService.paySuccess(orderId,trade_no, Constants.PAY_TYPE_1);
                }
            }else if("WAIT_BUYER_PAY".equals(trade_status)) {
                System.out.println("二维码已扫描，等侍支付");
            }

            System.out.println(parameterMap);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            System.out.println("签名异常");
        }

    }


    /**
     * 获取request中的参数集合转map
     * @param request
     * @return
     */
    public Map<String,String> getParameterMap(HttpServletRequest request){
        Map<String,String> map = new HashMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()){
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if(paramValues.length ==1){
                String paramValue = paramValues[0];
               if(paramValue.length() !=0){
                   map.put(paramName,paramValue);
               }
            }
        }
        return map;
    }
}

